//
//  ComicsHead.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/14.
//  Copyright © 2017年 tml. All rights reserved.
//


import UIKit


class ComicsHead: UIView {
    fileprivate lazy var bgView: UIImageView = {
       let imgV = UIImageView()
        imgV.isUserInteractionEnabled = true
        imgV.contentMode = .scaleAspectFill
        imgV.blurView.setup(style: .dark, alpha: 1).enable()
        return imgV
    }()
    
    fileprivate lazy var coverView: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.addBorder(width: 3, color: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))
        imgV.layer.cornerRadius = 3
        return imgV
    }()
    
    fileprivate lazy var nameLabel: UILabel = {
       let label = UILabel()
        label.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        label.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    
    fileprivate lazy var authorLabel: UILabel = {
       let label = UILabel()
        label.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        label.font = UIFont.systemFont(ofSize: 13)
        return label
    }()
    
    fileprivate lazy var totalLabel: UILabel = {
        let label = UILabel()
        label.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        label.font = UIFont.systemFont(ofSize: 13)
        return label
    }()
    
    fileprivate lazy var thmemView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 5
        layout.itemSize = CGSize(width: 40, height: 20)
       let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(ComicsHeadCell.self, forCellWithReuseIdentifier: "ComicsHeadCell")
        return collectionView
    }()
    
    fileprivate var themes:[String]?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.bgView)
        self.bgView.addSubview(self.coverView)
        self.bgView.addSubview(self.nameLabel)
        self.bgView.addSubview(self.authorLabel)
        self.bgView.addSubview(self.totalLabel)
        self.bgView.addSubview(self.thmemView)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setUpUI() {
        self.bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.coverView.snp.makeConstraints { (make) in
            make.left.bottom.equalToSuperview().inset(UIEdgeInsetsMake(0, 20, 20, 0))
            make.width.equalTo(90)
            make.height.equalTo(120)
        }
        
        self.nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.coverView.snp.right).offset(20)
            make.right.greaterThanOrEqualToSuperview().offset(-20)
            make.top.equalTo(self.coverView)
            make.height.equalTo(20)
        }
        
        self.authorLabel.snp.makeConstraints { (make) in
            make.left.height.equalTo(nameLabel)
            make.right.greaterThanOrEqualToSuperview().offset(-20)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
        }
        
        self.totalLabel.snp.makeConstraints { (make) in
            make.left.height.equalTo(authorLabel)
            make.right.greaterThanOrEqualToSuperview().offset(-20)
            make.top.equalTo(authorLabel.snp.bottom).offset(10)
        }
        
        self.thmemView.snp.makeConstraints { (make) in
            make.left.equalTo(totalLabel)
            make.height.equalTo(30)
            make.right.greaterThanOrEqualToSuperview().offset(-20)
            make.bottom.equalTo(coverView)
        }
    }
    
    var detailStatic: ComicsStaticModel? {
        didSet {
            guard let model = detailStatic else {
                return
            }
            bgView.kf.setImage(urlString: model.cover, placeholder: #imageLiteral(resourceName: "normal_placeholder_v"))
            coverView.kf.setImage(urlString: model.cover, placeholder: #imageLiteral(resourceName: "normal_placeholder_v"))
            nameLabel.text = model.name
            authorLabel.text = model.author?.name
            themes = model.theme_ids
            thmemView.reloadData()
        }
    }
    
    var detailRealtime: ComicRealtimeModel? {
        didSet{
            guard let detailRealtime = detailRealtime else { return }
            let text = NSMutableAttributedString(string: "点击 收藏")
            
            text.insert(NSAttributedString(string: " \(detailRealtime.click_total ?? "0") ",
                attributes: [NSAttributedStringKey.foregroundColor: UIColor.orange,
                             NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)]), at: 2)
            
            text.append(NSAttributedString(string: " \(detailRealtime.favorite_total ?? "0") ",
                attributes: [NSAttributedStringKey.foregroundColor: UIColor.orange,
                             NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)]))
            totalLabel.attributedText = text
        }
    }
    
}

class ComicsHeadCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.addBorder(width: 1, color: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))
        self.contentView.layer.cornerRadius = 3
        self.contentView.addSubview(self.titleLabel)
        setUpSnap()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var titleLabel: UILabel = {
       let label = UILabel()
        label.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    fileprivate func setUpSnap() {
        self.titleLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
}

extension ComicsHead: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return themes?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ComicsHeadCell", for: indexPath) as! ComicsHeadCell
        cell.titleLabel.text = themes?[indexPath.row]
        return cell
    }
    
    
}



